package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.domain.User;
import com.example.demo.untils.JwtConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

@RestController
public class LoginController {

    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    JwtConfig jwtConfig;

    @GetMapping("/user/login")
    public String login(String userName,String password){
        System.out.println(userName);
        System.out.println(password);
        Authentication authenticate =authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(userName, password,new ArrayList<>()));
        User user=new User();
        user.setUserName(userName);
        user.setPassword(password);
        String s = JSON.toJSONString(user);
        String token = jwtConfig.createToken(s);
        return  token;
    }
    @GetMapping("/aa")
    public String aa(){
        return  "这是上传后的12112dsdas1111";
    }

}
